package org.credo.io.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MMSTest implements Serializable {

	private static final String TXTMSG = "纯中文测试！！！王磊去开车了!啊!";
	private static final String SMILMSG = "我<smil xmlns=\"http://www.w3.org/2000/SMIL20/CR/Language\"><head><layout><root-layout width=\"208\" height=\"176\" /><region id=\"Image\" left=\"0\" top=\"0\" width=\"128\" height=\"128\" fit=\"hidden\" /><region id=\"Text\" left=\"0\" top=\"50\" width=\"128\" height=\"128\" fit=\"hidden\" /></layout></head><body><par dur=\"25000ms\"><text src=\"1.txt\" region=\"Text\" /></par></body></smil>";

	private static final long serialVersionUID = 4895040820618838763L;

	/**
	 * 压缩文件
	 * @param srcfile
	 * @param zipfile
	 */
	public static void zipFiles(File[] srcfile, File zipfile) {
		byte[] buf = new byte[1024];
		try {
			// ZipOutputStream类：完成文件或文件夹的压缩
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
			for (int i = 0; i < srcfile.length; i++) {
				FileInputStream in = new FileInputStream(srcfile[i]);
				out.putNextEntry(new ZipEntry(srcfile[i].getName()));
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.closeEntry();
				in.close();
			}
			out.close();
			System.out.println("压缩完成.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 1.建立一个txt,输入msg. 2.建议一个smil文件,格式无所谓. 3.apache zip压缩目录下文件.
	 * 4.file对象,存放于绝对路径下的ZipMessage\System.currentTime目录下,避免冲突. 5.字节流发送.
	 * 
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// File file=new File("C://ZipMessage//");
		//
		// if(!file.exists()){
		// file.mkdirs();
		// }

		// 第一个文件1.txt
		PrintWriter pw = new PrintWriter(new FileWriter("1.txt"));
		pw.print(TXTMSG);
		pw.close();
		// File filename = new File("C:\\ZipMessage\\1.txt");
		// String filein = "啊啊啊纯中文测试！！！";
		// RandomAccessFile mm = null;
		// try {
		// mm = new RandomAccessFile(filename, "rw");
		// mm.writeUTF(filein);
		// //mm.writeBytes(filein);
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// } finally {
		// if (mm != null) {
		// try {
		// mm.close();
		// } catch (IOException e2) {
		// e2.printStackTrace();
		// }
		// }
		// }
		//
		// //第二个文件 test.smil
		PrintWriter pw2 = new PrintWriter(new FileWriter("test.smil"));
		pw2.print(SMILMSG);
		pw2.close();
		// File smailFile = new File("C:\\ZipMessage\\test.smil");
		// RandomAccessFile mm2 = null;
		// try {
		// mm2 = new RandomAccessFile(smailFile, "rw");
		// mm2.writeUTF(SMILMSG);
		// //mm.writeBytes(filein);
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// } finally {
		// if (mm2 != null) {
		// try {
		// mm2.close();
		// } catch (IOException e2) {
		// e2.printStackTrace();
		// }
		// }
		// }

		// 压缩
		File f1 = new File("1.txt");
		File f2 = new File("test.smil");
		File[] srcfile = { f1, f2 };
		// 压缩后的文件
		File zipfile = new File("biao.zip");
		MMSTest.zipFiles(srcfile, zipfile);

	}

}
